<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Ticket info page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript">
        function saveRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "added":
                    alert("Ticket info added!")
                    break;
                case "updated":
                    alert("Ticket info updated");
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the save response");
                    break;
            }
        }

        function queryRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "deleted":
                    alert('Deleted ticket info');
                    resetForm();
                    break;
                case "success":
                    //alert('Successful query');
                    try {
                        ticket_rate=xmlDoc.getElementsByTagName("ticket_rate");
                        document.getElementById("ticket_rate").value=ticket_rate[0].firstChild.nodeValue;
                        coupon_value=xmlDoc.getElementsByTagName("coupon_value");
                        document.getElementById("coupon_value").value=coupon_value[0].firstChild.nodeValue;
                        l0=xmlDoc.getElementsByTagName("l0");
                        document.getElementById("l0").value=l0[0].firstChild.nodeValue;
                        l1=xmlDoc.getElementsByTagName("l1");
                        document.getElementById("l1").value=l1[0].firstChild.nodeValue;
                        l2=xmlDoc.getElementsByTagName("l2");
                        document.getElementById("l2").value=l2[0].firstChild.nodeValue;
                    } catch (er) {
                        alert('Invalid ticket rate');
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }
        }

        function dispTableHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            table_text = "<button onclick='displayTable()'>Display the ticket info table</button>";
            table_text = table_text + "<table border='1'>"+
                      "<tr><th>ID</th>"+
                          "<th>Ticket Rate</th>"+
                          "<th>Coupon Value</th>"+
                          "<th>Level 0</th>"+
                          "<th>Level 1</th>"+
                          "<th>Level 2</th></tr>";

            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    producers=xmlDoc.getElementsByTagName("ticket");
                    for (i=0;i<producers.length;i++) {
                        table_text = table_text + "<tr>";
                        try {
                            table_text = table_text + insertField(producers[i], "ticket_id");
                            table_text = table_text + insertField(producers[i], "ticket_rate");
                            table_text = table_text + insertField(producers[i], "coupon_value");
                            table_text = table_text + insertField(producers[i], "l0");
                            table_text = table_text + insertField(producers[i], "l1");
                            table_text = table_text + insertField(producers[i], "l2");
                        } catch (er) {
                            alert('Invalid ticket rate');
                        }
                        table_text = table_text + "</tr>";
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }

            table_text = table_text + "</table>";
            document.getElementById("ticket_table").innerHTML=table_text;
        }

        function validateForm() {
            var return_value = true;
            var ticket_rate=document.getElementById("ticket_rate").value;
            var coupon_value=document.getElementById("coupon_value").value;
            var l0=document.getElementById("l0").value;
            var l1=document.getElementById("l1").value;
            var l2=document.getElementById("l2").value;

            if (!ticket_rate) {
                document.getElementById("ticket_rate_hint").innerHTML =
                    "<p style='color:red'><b>Ticket rate cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("ticket_rate_hint").innerHTML = " ";
            }

            if (!coupon_value) {
                document.getElementById("coupon_value_hint").innerHTML =
                    "<p style='color:red'><b>Coupon value cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("coupon_value_hint").innerHTML = " ";
            }

            if (!l0) {
                document.getElementById("l0_hint").innerHTML =
                    "<p style='color:red'><b>This field cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("l0_hint").innerHTML = " ";
            }

            if (!l1) {
                document.getElementById("l1_hint").innerHTML =
                    "<p style='color:red'><b>This field cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("l1_hint").innerHTML = " ";
            }

            if (!l2) {
                document.getElementById("l2_hint").innerHTML =
                    "<p style='color:red'><b>This field cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("l2_hint").innerHTML = " ";
            }

            return return_value;

        }

        function saveForm() {
            if (!validateForm()) {
                alert('Correct errors in the form before trying again!');
                return;
            }

            var ticket_rate=document.getElementById("ticket_rate").value;
            var coupon_value=document.getElementById("coupon_value").value;
            var l0=document.getElementById("l0").value;
            var l1=document.getElementById("l1").value;
            var l2=document.getElementById("l2").value;

            srvr_request="saveticket.php?ticket_rate="+ticket_rate+
                            "&coupon_value="+coupon_value+
                            "&l0="+l0+
                            "&l1="+l1+
                            "&l2="+l2;

            ajaxCall(srvr_request,saveRespHdlr);
        }

        function resetForm() {
            document.getElementById("ticket_rate").value = "";
            document.getElementById("ticket_rate_hint").innerHTML = "";
            document.getElementById("coupon_value").value = "";
            document.getElementById("coupon_value_hint").innerHTML = "";
            document.getElementById("l0").value = "";
            document.getElementById("l0_hint").innerHTML = "";
            document.getElementById("l1").value = "";
            document.getElementById("l1_hint").innerHTML = "";
            document.getElementById("l2").value = "";
            document.getElementById("l2_hint").innerHTML = "";
        }

        function queryForm() {
            var ticket_rate=document.getElementById("ticket_rate").value;
            srvr_request="queryticket.php?ticket_rate="+ticket_rate;
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function deleteForm() {
            var ticket_rate=document.getElementById("ticket_rate").value;
            srvr_request="queryticket.php?ticket_rate="+ticket_rate+
                        "&action=delete";
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function displayTable() {
            srvr_request="queryticket.php";
            ajaxCall(srvr_request,dispTableHdlr);
        }

        function checkLoginCookie() {
            session_auth=getCookie('session_auth');
            logged_username=getCookie('username');

            if(session_auth!='kteam_auth' || !logged_username) {
                setCookie('redirector','ticket.html',60*60*1000);
                window.location = "login.html";
            }
        }

    </script>

  </head>
  <body onLoad="checkLoginCookie()">
      <table border="1">
          <tr><td width="150"><a href="producer.html"><b>Producers</b></a></td>
              <td width="150"><a href="film.html"><b>Films</b></a></td>
              <td width="150"><a href="centre.html"><b>Centres</b></a></td>
              <td width="150"><a href="agent.html"><b>Agents</b></a></td>
              <td width="150"><a href="customer.html"><b>Tickets</b></a></td>
              <td width="150"><a href="ticket.html"><b>Ticket rate information</b></a></td>
              <td width="150"><a href="ticketstatus.html"><b>Status of ticket sales for each film</b></a></td>
              <td width="150"><a href="customerstatus.html"><b>Status for each customer</b></a></td>
              <td width="150"><a href="logout.php"><b>Logout</b></a></td></tr>
      </table>

      <table border="0">

            <tr><td><label>Ticket rate&nbsp;*</label></td>
                <td><input type=text id=ticket_rate size='20' value=''></td>
                <td><div id="ticket_rate_hint"> </div></td></tr>

            <tr><td><label>Coupon value&nbsp;*</label></td>
                <td><input type=text id=coupon_value size='20' value=''></td>
                <td><div id="coupon_value_hint"> </div></td></tr>

            <tr><td><label>Level 0&nbsp;*</label></td>
                <td><input type=text id=l0 size='20' value=''></td>
                <td><div id="l0_hint"> </div></td></tr>

            <tr><td><label>Level 1&nbsp;*</label></td>
                <td><input type=text id=l1 size='20' value=''></td>
                <td><div id="l1_hint"> </div></td></tr>

            <tr><td><label>Level 2&nbsp;*</label></td>
                <td><input type=text id=l2 size='20' value=''></td>
                <td><div id="l2_hint"> </div></td></tr>

        </table>
        <table>
            <tr><td><input type="submit" onClick="saveForm()" value="Save" /></td>
                <td><input type="submit" onClick="resetForm()" value="Reset" /></td>
                <td><input type="submit" onClick="queryForm()" value="Query" /></td>
                <td><input type="submit" onClick="deleteForm()" value="Delete" /></td></tr>

        </table>
        <div id="ticket_table">
            <button onclick="displayTable()">Display the ticket info table</button>
        </div>
  </body>
</html>
